/**
 * @author cprobert
 */
var sonicUI = {
	msg: "",
	init: function(){
		sonicAlert.init(); 
		sonicAlert.trace("sonicAlert.arePrefsSet = "+ sonicAlert.arePrefsSet);
		
		if(!sonicAlert.arePrefsSet)
			sonicUI.showPrefs();
			
		listenUp.init(); //Set ajax polling
		
		if (air.NativeApplication.supportsSystemTrayIcon) 
			this.setupUI();
		else {
			window.nativeWindow.visible = true;
			window.nativeWindow.minimize(); // Start minimised
		}
		
		this.wireEvents();
	},
	setupUI: function(){
			window.nativeWindow.visible = false;
		
			air.NativeApplication.nativeApplication.autoExit = false;
			var iconLoad = new air.Loader();
			var iconMenu = new air.NativeMenu();
			
			var openPrefs = iconMenu.addItem(new air.NativeMenuItem("Preferences"));
			openPrefs.addEventListener(air.Event.SELECT, function(event){
				sonicUI.showPrefs();
			});
			
			var updateNow = iconMenu.addItem(new air.NativeMenuItem("Last message"));
			updateNow.addEventListener(air.Event.SELECT, function(event){
				msg.toast(msg.text);
			});
			
			var stopTimer = iconMenu.addItem(new air.NativeMenuItem("Stop"));
			stopTimer.addEventListener(air.Event.SELECT, function(event){
				listenUp.stop();
			});
			
			var reloadApp = iconMenu.addItem(new air.NativeMenuItem("Start"));
			reloadApp.addEventListener(air.Event.SELECT, function(event){	
				sonicUI.reload();
			});
			
			/*
			var playSound = iconMenu.addItem(new air.NativeMenuItem("Play"));
			playSound.addEventListener(air.Event.SELECT, function(event){
				sonicAlert.playSound(prefs.soundFile);
			});
			*/
			
			var exitCommand = iconMenu.addItem(new air.NativeMenuItem("Exit"));
			exitCommand.addEventListener(air.Event.SELECT, function(event){
				air.NativeApplication.nativeApplication.icon.bitmaps = [];
				sonicUI.exit();
			});
			
			if (air.NativeApplication.supportsSystemTrayIcon) {
				air.NativeApplication.nativeApplication.autoExit = false;
				iconLoad.contentLoaderInfo.addEventListener(air.Event.COMPLETE, sonicUI.iconLoadComplete);
				iconLoad.load(new air.URLRequest("icons/speaker_16.png"));
				air.NativeApplication.nativeApplication.icon.tooltip = "Sonic Alert";
				air.NativeApplication.nativeApplication.icon.menu = iconMenu;
			}
			
			if (air.NativeApplication.supportsDockIcon) {
				iconLoad.contentLoaderInfo.addEventListener(air.Event.COMPLETE, sonicUI.iconLoadComplete);
				iconLoad.load(new air.URLRequest("icons/speaker_128.png"));
				air.NativeApplication.nativeApplication.icon.menu = iconMenu;
			}
	},
	wireEvents: function(){
		
		$("input#btnPlay").click(function(){
			sonicAlert.playSound(prefs.soundFile);
		});
		$("input#btnExit").click(function(){
			sonicUI.exit();
		});
		$("input#btnPrefs").click(function(){
			sonicUI.showPrefs();
		});
	},
	iconLoadComplete: function(event){
		air.NativeApplication.nativeApplication.icon.bitmaps = [event.target.content.bitmapData];
	},
	exit: function(){
		sonicAlert.trace("Exiting app");
		air.NativeApplication.nativeApplication.exit();
	},
	reload: function(){
		sonicAlert.trace("Reloading Sonic Alert");
		window.location.reload(true);
	},
	showPrefs: function(){
		sonicAlert.trace("Showing preferences");
		
		var options = new air.NativeWindowInitOptions();
		options.systemChrome = air.NativeWindowSystemChrome.STANDARD;
		options.type = air.NativeWindowType.NORMAL;
		options.transparent = false;
		
		var windowBounds = new air.Rectangle( 10, 10, 380, 300 );
		var newHTMLLoader = air.HTMLLoader.createRootWindow( true, options, true, windowBounds );
		//newHTMLLoader.addEventListener( air.Event.COMPLETE, sonicUI.reload );
		var prefsPage = air.File.applicationDirectory.resolvePath( "prefs.html" );
		newHTMLLoader.load(new air.URLRequest( prefsPage.url ));
		newHTMLLoader.window.opener = window; 
		/*
		newWindow = window.open("prefs.html", "logWindow", "height=200, width=350, top=10, left=10");
		*/
	}
};
